<template>
    <el-dialog :title="form.invitationId ? $t('common.editBtn') : $t('common.addBtn')" v-model="visible"
        :close-on-click-modal="false" draggable>
      <el-form ref="dataFormRef" :model="form" :rules="dataRules" label-width="90px" v-loading="loading">
        <el-row :gutter="24">
    <el-col :span="12" class="mb20">
  <el-form-item :label="t('CoalInvitation.userInfoId')" prop="userInfoId">
    <el-input-number :min="1" :max="1000" v-model="form.userInfoId" :placeholder="t('CoalInvitation.inputUserInfoIdTip')"></el-input-number>
  </el-form-item>
  </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item :label="t('CoalInvitation.createUserName')" prop="createUserName">
        <el-input v-model="form.createUserName" :placeholder="t('CoalInvitation.inputCreateUserNameTip')"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item :label="t('CoalInvitation.createUserPhoto')" prop="createUserPhoto">
        <el-input type="textarea" v-model="form.createUserPhoto" :placeholder="t('CoalInvitation.inputCreateUserPhotoTip')"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item :label="t('CoalInvitation.invitationText')" prop="invitationText">
        <el-input type="textarea" v-model="form.invitationText" :placeholder="t('CoalInvitation.inputInvitationTextTip')"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item :label="t('CoalInvitation.invitationTitle')" prop="invitationTitle">
        <el-input type="textarea" v-model="form.invitationTitle" :placeholder="t('CoalInvitation.inputInvitationTitleTip')"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item :label="t('CoalInvitation.invitationType')" prop="invitationType">
        <el-input v-model="form.invitationType" :placeholder="t('CoalInvitation.inputInvitationTypeTip')"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item :label="t('CoalInvitation.isTop')" prop="isTop">
        <el-input v-model="form.isTop" :placeholder="t('CoalInvitation.inputIsTopTip')"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item :label="t('CoalInvitation.topStartDate')" prop="topStartDate">
            <el-date-picker type="datetime" :placeholder="t('CoalInvitation.inputTopStartDateTip')" v-model="form.topStartDate" :value-format="dateTimeStr"></el-date-picker>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
  <el-form-item :label="t('CoalInvitation.likesCount')" prop="likesCount">
    <el-input-number :min="1" :max="1000" v-model="form.likesCount" :placeholder="t('CoalInvitation.inputLikesCountTip')"></el-input-number>
  </el-form-item>
  </el-col>

    <el-col :span="12" class="mb20">
  <el-form-item :label="t('CoalInvitation.collectCount')" prop="collectCount">
    <el-input-number :min="1" :max="1000" v-model="form.collectCount" :placeholder="t('CoalInvitation.inputCollectCountTip')"></el-input-number>
  </el-form-item>
  </el-col>

    <el-col :span="12" class="mb20">
  <el-form-item :label="t('CoalInvitation.browseCount')" prop="browseCount">
    <el-input-number :min="1" :max="1000" v-model="form.browseCount" :placeholder="t('CoalInvitation.inputBrowseCountTip')"></el-input-number>
  </el-form-item>
  </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item :label="t('CoalInvitation.contactPhone')" prop="contactPhone">
        <el-input v-model="form.contactPhone" :placeholder="t('CoalInvitation.inputContactPhoneTip')"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item :label="t('CoalInvitation.state')" prop="state">
        <el-input v-model="form.state" :placeholder="t('CoalInvitation.inputStateTip')"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item :label="t('CoalInvitation.createDate')" prop="createDate">
        <el-input v-model="form.createDate" :placeholder="t('CoalInvitation.inputCreateDateTip')"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item :label="t('CoalInvitation.invitationLabel')" prop="invitationLabel">
        <el-input v-model="form.invitationLabel" :placeholder="t('CoalInvitation.inputInvitationLabelTip')"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item :label="t('CoalInvitation.releaseDate')" prop="releaseDate">
        <el-input v-model="form.releaseDate" :placeholder="t('CoalInvitation.inputReleaseDateTip')"/>
      </el-form-item>
      </el-col>

    </el-row>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="visible = false">{{ $t('common.cancelButtonText') }}</el-button>
          <el-button type="primary" @click="onSubmit" :disabled="loading">{{ $t('common.confirmButtonText') }}</el-button>
        </span>
      </template>
    </el-dialog>
</template>

<script setup lang="ts" name="CoalInvitationDialog">
import { useDict } from '/@/hooks/dict';
import { useMessage } from "/@/hooks/message";
import { getObj, addObj, putObj } from '/@/api/coalSystem/CoalInvitation'
import { useI18n } from "vue-i18n"
import { rule } from '/@/utils/validate';
const emit = defineEmits(['refresh']);

const { t } = useI18n();

// 定义变量内容
const dataFormRef = ref();
const visible = ref(false)
const loading = ref(false)
// 定义字典

// 提交表单数据
const form = reactive({
    userInfoId: '',
    createUserName: '',
    createUserPhoto: '',
    invitationText: '',
    invitationTitle: '',
    invitationType: '',
    isTop: '',
    topStartDate: '',
    likesCount: '',
    collectCount: '',
    browseCount: '',
    contactPhone: '',
    state: '',
    createDate: '',
    invitationLabel: '',
    releaseDate: '',
});

// 定义校验规则
const dataRules = ref({
})

// 打开弹窗
const openDialog = (id: string) => {
  visible.value = true
  form.invitationId = ''

  // 重置表单数据
	nextTick(() => {
		dataFormRef.value?.resetFields();
	});

  // 获取coalInvitation信息
  if (id) {
    form.invitationId = id
    getcoalInvitationData(id)
  }
};

// 提交
const onSubmit = async () => {
	const valid = await dataFormRef.value.validate().catch(() => {});
	if (!valid) return false;

	try {
        loading.value = true;
		form.id ? await putObj(form) : await addObj(form);
		useMessage().success(t(form.id ? 'common.editSuccessText' : 'common.addSuccessText'));
		visible.value = false;
		emit('refresh');
	} catch (err: any) {
		useMessage().error(err.msg);
	} finally {
        loading.value = false;
    }
};

// 初始化表单数据
const getcoalInvitationData = (id: string) => {
  // 获取数据
  loading.value = true
  getObj(id).then((res: any) => {
    Object.assign(form, res.data)
  }).finally(() => {
    loading.value = false
  })
};

// 暴露变量
defineExpose({
  openDialog
});
</script>